//
//  MHSerchViewController.m
//  MiaoCircle
//
//  Created by 许正荣 on 2018/5/14.
//  Copyright © 2018年 acgera. All rights reserved.
//

#import "MHSerchViewController.h"
#import "MHCustomSearchBar.h"
#import "MHSearchViewModel.h"
#import "MHSearchResultTableViewCell.h"
#import "MHCommonHeaderView.h"
#import "MHSearchBar.h"


@interface MHSerchViewController ()<UISearchBarDelegate>
@property(nonatomic, strong) MHSearchViewModel *viewModel;
@property(nonatomic, strong) MHCustomSearchBar *searhBar;
@end

@implementation MHSerchViewController
@dynamic viewModel;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.emptyDataSetSource = nil;
    self.tableView.emptyDataSetDelegate = nil;
    UIBarButtonItem *customBackButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@""] style:UIBarButtonItemStylePlain target:self action:nil];
    self.navigationItem.leftBarButtonItem = customBackButton;
    self.tableView.backgroundColor = MH_MAIN_BACKGROUNDCOLOR_2;
//    self.navigationItem.leftBarButtonItems = nil;
//    UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithCustomView:[[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 1)]];
//    // Width equivalent to system default Done button's (which appears on pushed view in my case).
//    leftBarItem.enabled = NO;
//    self.navigationItem.leftBarButtonItem = leftBarItem;
    
    MHCustomSearchBar *searchBar = [[MHCustomSearchBar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 44)];
    searchBar.delegate = self;
//    searchBar.tintColor = MHColorFromHexString(@"#f0f0f0");
    searchBar.placeholder = @"搜索";
    searchBar.showsCancelButton = YES;
    self.searhBar = searchBar;
    
//    for(UIView *view in  [[[searchBar subviews] objectAtIndex:0] subviews]) {
//        if([view isKindOfClass:[NSClassFromString(@"UINavigationButton") class]]) {
//        UIButton * cancel =(UIButton *)view;
//        [cancel setTitle:@"搜索" forState:UIControlStateNormal];
//        cancel.titleLabel.font = [UIFont systemFontOfSize:14];
//    }
//    }
    
//    [searchBar setValue:@"搜索" forKey:@"_cancelButtonText"];
    
    //启动按钮

    
//    [btnCancel setTitle:@"确定" forState:0];
////    btnCancel.selected = YES;
//    [btnCancel setTitleColor:MHWhiteTextColor forState:UIControlStateNormal];
//    [btnCancel setTitleColor:MHWhiteTextColor forState:UIControlStateHighlighted];
////    [btnCancel setTitleColor: forState:UIControlStateSelected];
//    [btnCancel setNeedsLayout];
    self.navigationItem.titleView = searchBar;
     [self.tableView mh_registerNibCell:MHSearchResultTableViewCell.class];
    self.definesPresentationContext = YES;
  
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    UIButton *btnCancel = [self.searhBar valueForKey:@"_cancelButton"];
    btnCancel.enabled = YES;
}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
//    self.searchController.active = YES;
}

-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [self.viewModel.popCommand execute:nil];
}

- (UIEdgeInsets)contentInset {
    return isIPhoneX() ? UIEdgeInsetsMake(100, 0, 83, 0) : UIEdgeInsetsMake(64, 0, 49, 0);
}



//-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    MHCommonHeaderView *headerView = [MHCommonHeaderView headerViewWithTableView:tableView];
//    headerView.backgroundColor = MHColor(170, 207, 241);
//    headerView.contentLabel.text = @"主题";
//    return  headerView;
//}

- (UITableViewCell *)tableView:(UITableView *)tableView dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath{
    return [tableView dequeueReusableCellWithIdentifier:@"MHSearchResultTableViewCell"];
}

- (void)configureCell:(MHSearchResultTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath withObject:(id)object{
    [cell bindViewModel:object];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    self.viewModel.dataSource = @[];
    self.viewModel.searchText = searchText;
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
    if (searchBar.text.length > 0) {
        [searchBar resignFirstResponder];
        [self.viewModel.requestRemoteDataCommand execute:@(1)];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


//-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//    return 30;
//}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

@end
